package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.enums.DefaultValueEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ReceiptOrderEnum;
import com.xbongbong.saas.enums.product.ReceiptOrderServiceEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author WuHB
 * @date 2021/11/18 09:11
 */
@Getter
public enum TempReceiptOrderEnum {

    // ==================== 基础信息 ====================
    BASE_INFO(ReceiptOrderEnum.BASE_INFO),
    // 回执单编号
    RECEIPT_ORDER_NO(ReceiptOrderEnum.RECEIPT_ORDER_NO),
    // 关联工单
    LIKE_WORK_ORDER(ReceiptOrderEnum.LINK_WORK_ORDER),
    // 关联客户
    LINK_CUSTOMER(ReceiptOrderEnum.LINK_CUSTOMER),
    // 回执信息
    RECEIPT_INFO(ReceiptOrderEnum.RECEIPT_INFO),
    // ==================== 销售信息 ====================
    SALES_INFO(ReceiptOrderEnum.SALES_INFO),
    // 关联产品
    PRODUCT(ReceiptOrderEnum.PRODUCT),
    // 服务项目
    SERVICE_PROJECT(ReceiptOrderEnum.SERVICE_PROJECT),
    // 产品合计
    PRODUCT_TOTAL(ReceiptOrderEnum.PRODUCT_TOTAL),
    // 服务项目合计
    SERVICE_TOTAL(ReceiptOrderEnum.SERVICE_TOTAL),
    // 总费用
    TOTAL_AMOUNT(ReceiptOrderEnum.TOTAL_AMOUNT),
    // 负责人
    OWNER_ID(ReceiptOrderEnum.OWNER_ID),
    // 协同人
    MINOR_USER(ReceiptOrderEnum.MINOR_USER),
    // 协同人
    SETTLE_STATUS(ReceiptOrderEnum.SETTLE_STATUS),
    // ==================== 系统信息 ====================
    SYSTEM_INFO(ReceiptOrderEnum.SYSTEM_INFO),
    // 创建人
    CREATOR_ID(ReceiptOrderEnum.CREATOR_ID),
    // 创建时间
    ADD_TIME(ReceiptOrderEnum.ADD_TIME),
    // 修改时间
    UPDATE_TIME(ReceiptOrderEnum.UPDATE_TIME),
    // ==================== 隐藏字段 ====================
    // 关联客户隐藏
    LINK_CUSTOMER_HIDE(ReceiptOrderEnum.LINK_CUSTOMER_HIDE),
    // 关联工单隐藏
    LINK_WORK_ORDER_HIDE(ReceiptOrderEnum.LINK_WORK_ORDER_HIDE),
    ;

    /**
     * 调整单枚举
     */
    private final ReceiptOrderEnum anEnum;


    TempReceiptOrderEnum(ReceiptOrderEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempReceiptOrderEnum> getAllEnum() {
        List<TempReceiptOrderEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    /**
     * 初始化回执单编号
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initReceiptOrderNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.RECEIPT_ORDER.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.RECEIPT_ORDER.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(RECEIPT_ORDER_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.RECEIPT_ORDER_NO));
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setStrictController(0);
        fieldAttrEntity.setShowEditable(1);
    }

    /**
     * 初始化关联工单
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initLinkWorkOrderV2(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.WORK_ORDER_V2.getCode());
        fieldAttrEntity.setLinkedText(LINK_WORK_ORDER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("workOrderV2");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setDesignEditable(0);
    }

    /**
     * 初始化关联客户
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initLinkCustomer(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(LINK_CUSTOMER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化产品
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initProducts(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化服务项目
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initServiceProject(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化负责人
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setMultiple(0);
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管】可删除负责人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.TREASURER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.TREASURER));
        // 设置默认值
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.DYNAMIC.getAlias());
        defaultAttrPoJo.setDefaultValue(DefaultValueEnum.LOGIN_USER.getValue());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }

    /**
     * 初始化协同人
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管】可删除负责人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.TREASURER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.TREASURER));
    }

    public static void initProductTotal(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setMemo("默认值为产品合计计");
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setEditable(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + ReceiptOrderEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + ReceiptOrderEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initServiceTotal(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setMemo("默认值为服务项目合计");
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setEditable(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + ReceiptOrderEnum.SERVICE_PROJECT.getAttr() + "." + ReceiptOrderServiceEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + ReceiptOrderEnum.SERVICE_PROJECT.getAttr() + "." + ReceiptOrderServiceEnum.SUBTOTAL.getAttr(), "关联服务.售价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initTotalAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("默认值=产品合计+服务项目合计");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "ADD(SUM({self." + ReceiptOrderEnum.PRODUCT_TOTAL.getAttr() + "}),SUM({self." + ReceiptOrderEnum.SERVICE_TOTAL.getAttr() + "}))";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + ReceiptOrderEnum.PRODUCT_TOTAL.getAttr(), ReceiptOrderEnum.PRODUCT_TOTAL.getAttrName());
        labelMap.put("self." + ReceiptOrderEnum.SERVICE_TOTAL.getAttr(), ReceiptOrderEnum.SERVICE_TOTAL.getAttrName());
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }


    /**
     * 初始化创建人
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    /**
     * 初始化添加时间
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    /**
     * 初始化修改时间
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
}
